package com.lb.activity.rest;

import cn.hutool.core.date.DateUtil;
import com.lb.activity.po.ExamineFlowStep;
import com.lb.activity.po.LeaveBill;
import com.lb.activity.service.LeaveBillService;
import com.lb.activity.util.ResultUtil;
import com.lb.activity.vo.LeaveBillVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;


/**
 * @author liubin
 */
@Api(value = "请假申请")
@RestController
@RequestMapping("person")
public class LeaveBillController {

    @Autowired
    private LeaveBillService leaveBillService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private HistoryService historyService;

    @GetMapping("leaveBill")
    @ApiOperation(value = "跳转请假申请页面")
    public ModelAndView qingjia(){
        return new ModelAndView("person/leave/leaveBill");
    }

    @GetMapping("leaveBillAdd")
    @ApiOperation(value = "跳转到添加请假界面")
    public ModelAndView leaveBillAdd(){
        ModelAndView modelAndView = new ModelAndView("person/leave/leaveBillAdd");
        return modelAndView;
    }

    @PostMapping("addLeaveBill")
    @ApiOperation(value = "开始请假申请")
    public ResultUtil addLeaveBill(LeaveBillVo leaveBillVo){
        ResultUtil resultUtil = leaveBillService.addLeaveBillI(leaveBillVo);
        return resultUtil;
    }

    @GetMapping("myLeaveBillList")
    @ApiOperation("我的申请集合")
    public ResultUtil myLeaveBillList(LeaveBillVo leaveBillVo){
        ResultUtil resultUtil = leaveBillService.myLeaveBillList(leaveBillVo);
        return resultUtil;
    }

    @GetMapping("leaveBillDetail")
    @ApiOperation(value = "查看任务进度")
    public void leaveBillDetail(String executionid, HttpServletResponse response) throws IOException {
        ProcessInstance process = runtimeService.createProcessInstanceQuery().processInstanceId(executionid).singleResult();
        BpmnModel bpmnmodel = repositoryService.getBpmnModel(process.getProcessDefinitionId());
        List<String> activeActivityIds = runtimeService.getActiveActivityIds(executionid);
        DefaultProcessDiagramGenerator gen = new DefaultProcessDiagramGenerator();
        // 获得历史活动记录实体（通过启动时间正序排序，不然有的线可以绘制不出来）
        List<HistoricActivityInstance> historicActivityInstances = historyService.createHistoricActivityInstanceQuery()
                .executionId(executionid).orderByHistoricActivityInstanceStartTime().asc().list();
        // 计算活动线
        List<String> highLightedFlows = leaveBillService
                .getHighLightedFlows(
                        (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
                                .getDeployedProcessDefinition(process.getProcessDefinitionId()),
                        historicActivityInstances);

        InputStream in = gen.generateDiagram(bpmnmodel, "png", activeActivityIds, highLightedFlows, "宋体", "宋体", null,
                null, 1.0);
        ServletOutputStream output = response.getOutputStream();
        IOUtils.copy(in, output);
    }

    // ****************************************部门领导审批**************************************************************

    @GetMapping("toDeptExamine")
    @ApiOperation(value = "部门领导审批")
    public ModelAndView toDeptExamine(){
        return new ModelAndView("examine/leaveExamine");
    }

    @GetMapping("leaveExamineList")
    @ApiOperation(value = "部门领导审批列表")
    public ResultUtil toDeptExamine(LeaveBillVo leaveBillVo){
        ResultUtil resultUtil = leaveBillService.deptLeaveBillList(leaveBillVo);
        return resultUtil   ;
    }

    @GetMapping("toLeaveBillDeal")
    @ApiOperation(value = "跳转到请假申请处理页面")
    public ModelAndView toLeaveBillDeal(String taskId){
        ModelAndView modelAndView = new ModelAndView("examine/leaveDeal");
        LeaveBill leaveBill = leaveBillService.getOneLeaveBill(taskId);
        List<ExamineFlowStep> stepList = leaveBillService.getExamineStep(leaveBill.getLeaveId());
        modelAndView.addObject("leaveBill",leaveBill);
        modelAndView.addObject("stepList",stepList);
        modelAndView.addObject("taskId",taskId);
        return modelAndView;
    }

    @PostMapping("examineDeptDeal")
    @ApiOperation(value = "部门领导审批")
    public ResultUtil examineDeptDeal(String taskId,String deptLeaderApprove){
        ResultUtil resultUtil = leaveBillService.complateLeaveBill(taskId,deptLeaderApprove);
        return resultUtil;
    }

//*****************************************人力审批***************************************************

    @GetMapping("toPersonExamine")
    @ApiOperation(value = "到人事经理审批页面")
    public ModelAndView toPersonExamine(){
        ModelAndView modelAndView = new ModelAndView("examine/leavePersonExamine");
        return modelAndView;
    }



    @GetMapping("leaveExaminePersonList")
    @ApiOperation(value = "人力领导审批列表")
    public ResultUtil leaveExaminePersonList(LeaveBillVo leaveBillVo){
        ResultUtil resultUtil = leaveBillService.deptLeaveBillPersonList(leaveBillVo);
        return resultUtil;
    }

    @GetMapping("toLeaveBillPersonDeal")
    @ApiOperation(value = "跳转到请假申请处理页面")
    public ModelAndView toLeaveBillPersonDeal(String taskId){
        ModelAndView modelAndView = new ModelAndView("examine/leavePersonDeal");
        LeaveBill leaveBill = leaveBillService.getOneLeaveBill(taskId);
        List<ExamineFlowStep> stepList = leaveBillService.getExamineStep(leaveBill.getLeaveId());
        modelAndView.addObject("leaveBill",leaveBill);
        modelAndView.addObject("stepList",stepList);
        modelAndView.addObject("taskId",taskId);
        return modelAndView;
    }

    @PostMapping("examinePersonDeal")
    @ApiOperation(value = "部门领导审批")
    public ResultUtil examinePersonDeal(String taskId,String hrapprove){
        ResultUtil resultUtil = leaveBillService.complateHrLeaveBill(taskId,hrapprove);
        return resultUtil;
    }


    //************************************销假 **************************************

    @GetMapping("myLeaveBillReportBackList")
    @ApiOperation(value = "获取销假列表")
    public ResultUtil myLeaveBillReportBackList(LeaveBillVo leaveBillVo){
        ResultUtil resultUtil = leaveBillService.myLeaveBillReportBackList(leaveBillVo);
        return resultUtil;
    }

    @GetMapping("toEditMyLeaveBillReportBack")
    @ApiOperation(value = "跳转销假页面")
    public ModelAndView toEditMyLeaveBillReportBack(String taskId){
        ModelAndView modelAndView = new ModelAndView("person/leave/leaveReport");
        LeaveBill leaveBill = leaveBillService.getOneLeaveBill(taskId);
        List<ExamineFlowStep> stepList = leaveBillService.getExamineStep(leaveBill.getLeaveId());
        modelAndView.addObject("leaveBill",leaveBill);
        modelAndView.addObject("stepList",stepList);
        modelAndView.addObject("taskId",taskId);
        return modelAndView;
    }

    @PostMapping("editMyLeaveBillReportBack")
    @ApiOperation(value = "编辑我的销假记录")
    public ResultUtil editMyLeaveBillReportBack(LeaveBillVo leaveBillVo){
        ResultUtil resultUtil = leaveBillService.editMyLeaveBillReportBack(leaveBillVo);
        return resultUtil;
    }


    // ************************************重新申请*******************************************

    @GetMapping("myLeaveBillApplyAgainList")
    @ApiOperation(value = "获取重新申请列表")
    public ResultUtil myLeaveBillApplyAgainList(LeaveBillVo leaveBillVo){
        ResultUtil resultUtil = leaveBillService.myLeaveBillApplyAgainList(leaveBillVo);
        return resultUtil;
    }


    @GetMapping("toEditMyLeaveBillApplyAgain")
    @ApiOperation(value = "跳转重新提交页面")
    public ModelAndView toEditMyLeaveBillApplyAgain(String taskId){
        ModelAndView modelAndView = new ModelAndView("person/leave/leaveApplyAgain");
        LeaveBill leaveBill = leaveBillService.getOneLeaveBill(taskId);
        LeaveBillVo leaveBillVo = new LeaveBillVo();
        BeanUtils.copyProperties(leaveBill,leaveBillVo);
        leaveBillVo.setApplyTimeStr(DateUtil.formatDate(leaveBill.getApplyDate()));
        leaveBillVo.setStartTimeStr(DateUtil.formatDate(leaveBill.getStartDate()));
        leaveBillVo.setEndTimeStr(DateUtil.formatDate(leaveBill.getEndDate()));
        List<ExamineFlowStep> stepList = leaveBillService.getExamineStep(leaveBill.getLeaveId());
        modelAndView.addObject("stepList",stepList);
        modelAndView.addObject("leaveBill",leaveBillVo);
        modelAndView.addObject("taskId",taskId);
        return modelAndView;
    }

    @PostMapping("editMyLeaveBillApplyAgain")
    @ApiOperation(value = "跳转重新提交页面")
    public ResultUtil editMyLeaveBillApplyAgain(LeaveBillVo leaveBillVo){
        ResultUtil resultUtil = leaveBillService.editMyLeaveBillApplyAgain(leaveBillVo);
        return resultUtil;
    }


    @GetMapping("myLeaveBillAll")
    @ApiOperation(value = "我的所有请假记录")
    public ResultUtil myLeaveBillAll(LeaveBillVo leaveBillVo){
        ResultUtil resultUtil = leaveBillService.myLeaveBillAll(leaveBillVo);
        return resultUtil;
    }


}
